package com.du.limiting.sentinel.runner;

import com.alibaba.csp.sentinel.slots.block.RuleConstant;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.FlowRuleManager;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRule;
import com.alibaba.csp.sentinel.slots.block.flow.param.ParamFlowRuleManager;
import com.alibaba.fastjson.JSONObject;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * @author dujunguang
 * @date 2022/2/23 11:07
 * @description: TODO
 **/
@Component
public class SentinelConfigRunner implements CommandLineRunner {


    @Override
    public void run(String... args) throws Exception {
        // 流控规则-QPS
        flowControlQps();
    }

    private void flowControlQps() {
        List<FlowRule> rules = new ArrayList<>();
        FlowRule rule1 = new FlowRule();
        rule1.setResource("flowControl");
        rule1.setCount(5);
        rule1.setGrade(RuleConstant.FLOW_GRADE_QPS);
        rules.add(rule1);
        FlowRuleManager.loadRules(rules);
        System.out.println(JSONObject.toJSONString(Collections.singletonList(rules)));
    }

}
